package com.example.viewdemo.b_r

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.os.Bundle
import android.view.View
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import com.example.viewdemo.databinding.ActivityBroadcastReceiverTestBinding

class BroadcastReceiverTest : AppCompatActivity(), View.OnClickListener {
    private lateinit var binding: ActivityBroadcastReceiverTestBinding
    lateinit var timeChangeReceiver: TimeChangeReceiver

    inner class TimeChangeReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            Toast.makeText(context, "Time has changed", Toast.LENGTH_SHORT).show()
        }
    }
    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(timeChangeReceiver)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()

        binding= ActivityBroadcastReceiverTestBinding.inflate(layoutInflater)
        setContentView(binding.root)

        binding.button9.setOnClickListener(this)
        val intentFilter = IntentFilter()
        intentFilter.addAction("android.intent.action.TIME_TICK")
        timeChangeReceiver = TimeChangeReceiver()
        registerReceiver(timeChangeReceiver, intentFilter)


    }

    override fun onClick(v: View?) {
        when(v){
            binding.button9  -> {
                val intent = Intent("com.example.broadcasttest.MY_BROADCAST")
                intent.setPackage(packageName)
                sendBroadcast(intent)
            }
        }
    }
}